<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【工作笔记】- APP 莫名崩溃，开始以为是 Header 中 name 大小写的锅，最后发现原来是容器的错！ | 程序员小航</title><meta name="keywords" content="工作笔记"><meta name="author" content="liuzhihang"><meta name="copyright" content="liuzhihang"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="在自己模拟过程中，才发现和容器有关系。">
<meta property="og:type" content="article">
<meta property="og:title" content="【工作笔记】- APP 莫名崩溃，开始以为是 Header 中 name 大小写的锅，最后发现原来是容器的错！">
<meta property="og:url" content="https://liuzhihang.com/2020/10/19/header-name-case.html">
<meta property="og:site_name" content="程序员小航">
<meta property="og:description" content="在自己模拟过程中，才发现和容器有关系。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/app-header-ydHubh.png">
<meta property="article:published_time" content="2020-10-18T23:00:00.000Z">
<meta property="article:modified_time" content="2020-12-27T09:23:14.596Z">
<meta property="article:author" content="liuzhihang">
<meta property="article:tag" content="工作笔记">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/app-header-ydHubh.png"><link rel="shortcut icon" href="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/favicon.ico"><link rel="canonical" href="https://liuzhihang.com/2020/10/19/header-name-case"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//www.google-analytics.com" crossorigin=""/><link rel="preconnect" href="//pingjs.qq.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google_site_verification" content="GZshAnnrC-4eCrl5h-e_5Rdk4lOUhRK7ShULoRi2q0E"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-126394362-1"></script><script>window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-126394362-1');
</script><script>var _mtac = {};
(function() {
    var mta = document.createElement("script");
    mta.src = "//pingjs.qq.com/h5/stats.js?v2.0.4";
    mta.setAttribute("name", "MTAH5");
    mta.setAttribute("sid", "66540676");
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(mta, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: {"limitDay":730,"position":"top","messagePrev":"It has been","messageNext":"days since the last update, the content of the article may be outdated."},
  highlight: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【工作笔记】- APP 莫名崩溃，开始以为是 Header 中 name 大小写的锅，最后发现原来是容器的错！',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-12-27 17:23:14'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="//at.alicdn.com/t/font_1608878_xsf93f0c1a.css"><meta name="generator" content="Hexo 5.2.0"><link rel="alternate" href="/atom.xml" title="程序员小航" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/404/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">145</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">55</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">31</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-layer-group"></i><span> 导航</span><i class="fas fa-chevron-down expand hide"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-bookmark"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 推荐</span><i class="fas fa-chevron-down expand hide"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/books/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-film"></i><span> 电影</span></a></li><li><a class="site-page child" href="/games/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/contact/"><i class="fa-fw fas fa-comments"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-address-book"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-user-circle"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 其他</span><i class="fas fa-chevron-down expand hide"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://juejin.im/user/1987506650493117"><i class="fa-fw iconfont iconjuejin-1"></i><span> 掘金</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/liuzhihang"><i class="fa-fw iconfont iconzhihu1"></i><span> 知乎</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.infoq.cn/profile/00B8AE08DA916E/publish"><i class="fa-fw iconfont iconweibiaoti-1"></i><span> InfoQ</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/qq_36535538"><i class="fa-fw iconfont iconcsdn"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://leetcode-cn.com/u/liuzhihang/"><i class="fa-fw iconfont iconleetcode"></i><span> LeetCode</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/app-header-ydHubh.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">程序员小航</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-layer-group"></i><span> 导航</span><i class="fas fa-chevron-down expand hide"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-bookmark"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 推荐</span><i class="fas fa-chevron-down expand hide"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/books/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-film"></i><span> 电影</span></a></li><li><a class="site-page child" href="/games/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/contact/"><i class="fa-fw fas fa-comments"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-address-book"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-user-circle"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 其他</span><i class="fas fa-chevron-down expand hide"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://juejin.im/user/1987506650493117"><i class="fa-fw iconfont iconjuejin-1"></i><span> 掘金</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/liuzhihang"><i class="fa-fw iconfont iconzhihu1"></i><span> 知乎</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.infoq.cn/profile/00B8AE08DA916E/publish"><i class="fa-fw iconfont iconweibiaoti-1"></i><span> InfoQ</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/qq_36535538"><i class="fa-fw iconfont iconcsdn"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://leetcode-cn.com/u/liuzhihang/"><i class="fa-fw iconfont iconleetcode"></i><span> LeetCode</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【工作笔记】- APP 莫名崩溃，开始以为是 Header 中 name 大小写的锅，最后发现原来是容器的错！</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-10-18T23:00:00.000Z" title="发表于 2020-10-19 07:00:00">2020-10-19</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-12-27T09:23:14.596Z" title="更新于 2020-12-27 17:23:14">2020-12-27</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%B7%A5%E4%BD%9C%E7%AC%94%E8%AE%B0/">工作笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>10分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【工作笔记】- APP 莫名崩溃，开始以为是 Header 中 name 大小写的锅，最后发现原来是容器的错！"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/2020/10/19/header-name-case.html#post-comment"><span class="gitalk-comment-count"></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p><strong>前言</strong></p>
<br>

<p>部署测试，部署预发布，一切测试就绪，上生产。</p>
<p>发布生产</p>
<p>闪退</p>
<p>What？？？</p>
<p>马上回滚</p>
<p>开始排查</p>
<p>后端一模一样的代码，不是 APP 端的问题吧。可 APP 端没有发版啊。</p>
<p>…… 一番排查</p>
<p>原来是 APP 端打包，测试和预发布包 Header 传的都是 <code>Authorization</code> ，生产传的是 <code>authorization</code> 。就是大小写问题，那赶紧改。</p>
</blockquote>
<h3 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h3><p>首页接口只有登录才可以进入，因为首页要展示获取用户账户的一些信息。这里使用的是统一拦截，从 Header 中获取 token 后，使用 token 获取用户信息。</p>
<p>而现在要改为用户未登录也可以查看首页信息中的宣传文案等等，只不过账户信息不显示。</p>
<h4 id="原流程"><a href="#原流程" class="headerlink" title="原流程"></a>原流程</h4><p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/FiwOie-8ZLjgv.png" alt="FiwOie-8ZLjgv"></p>
<p>整个过程代码在 <a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/8qY94y3w0Q2RRhkG7iUfOg">ThreadLocal底层原理</a> 里面有所介绍。这里省略一部分代码。</p>
<pre><code class="java">@Component
public class TokenInterceptor implements HandlerInterceptor &#123;

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception &#123;
        LocalUserUtils.remove();
    &#125;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception &#123;

        // 请求方法是否存在注解
        boolean assignableFrom = handler.getClass().isAssignableFrom(HandlerMethod.class);

        if (!assignableFrom) &#123;
            return true;
        &#125;

        CheckToken checkToken = null;
        if (handler instanceof HandlerMethod) &#123;
            checkToken = ((HandlerMethod) handler).getMethodAnnotation(CheckToken.class);
        &#125;

        // 没有加注解 直接放过
        if (checkToken == null) &#123;
            return true;
        &#125;

        // 从Header中获取Authorization
        String authorization = request.getHeader(&quot;Authorization&quot;);
        log.info(&quot;header authorization : &#123;&#125;&quot;, authorization);
        if (StringUtils.isBlank(authorization)) &#123;
            log.error(&quot;从Header中获取Authorization失败&quot;);
            throw CustomExceptionEnum.NOT_HAVE_TOKEN.throwCustomException();
        &#125;

        // 其他代码省略

        return true;
    &#125;
&#125;</code></pre>
<p>从代码中可以看出这里大概过程如下：</p>
<ol>
<li>是使用拦截器拦截请求</li>
<li>如果方法没有 CheckToken 注解直接放过</li>
<li>有 CheckToken 注解，则从 request 的 header 中获取 Authorization</li>
</ol>
<h4 id="新需求"><a href="#新需求" class="headerlink" title="新需求"></a>新需求</h4><p>这里想到只需要把注解去掉，然后从请求参数中获取 token 即可。获取到走原逻辑，获取不到则只返回宣传文案等信息。</p>
<h3 id="从-Header-中获取信息"><a href="#从-Header-中获取信息" class="headerlink" title="从 Header 中获取信息"></a>从 Header 中获取信息</h3><h4 id="直接获取请求头某一个-headerName"><a href="#直接获取请求头某一个-headerName" class="headerlink" title="直接获取请求头某一个 headerName"></a>直接获取请求头某一个 headerName</h4><pre><code class="java">@PostMapping(&quot;/getAuthorizationByKey&quot;)
public String getAuthorizationByKey(@RequestHeader(&quot;Authorization&quot;) String authorization) &#123;

    log.info(&quot;获取 Authorization ---&gt;&#123;&#125;&quot;, authorization);

    return authorization;
&#125;</code></pre>
<h4 id="使用-Map-获取所有请求头"><a href="#使用-Map-获取所有请求头" class="headerlink" title="使用 Map 获取所有请求头"></a>使用 Map 获取所有请求头</h4><pre><code class="java">@PostMapping(&quot;/getAuthorizationByMap&quot;)
public String getAuthorizationByMap(@RequestHeader Map&lt;String, String&gt; map) &#123;

    String authorization = map.get(&quot;Authorization&quot;);

    log.info(&quot;获取 Authorization ---&gt;&#123;&#125;&quot;, authorization);

    return authorization;
&#125;</code></pre>
<h4 id="使用-MultiValueMap-获取请求头"><a href="#使用-MultiValueMap-获取请求头" class="headerlink" title="使用 MultiValueMap 获取请求头"></a>使用 MultiValueMap 获取请求头</h4><pre><code class="java">@PostMapping(&quot;/getAuthorizationByMultiValueMap&quot;)
public String getAuthorizationByMultiValueMap(@RequestHeader MultiValueMap&lt;String, String&gt; map) &#123;

    List&lt;String&gt; authorization = map.get(&quot;Authorization&quot;);

    log.info(&quot;获取 Authorization ---&gt;&#123;&#125;&quot;, authorization);

    return &quot;SUCCESS&quot;;
&#125;</code></pre>
<h4 id="使用-HttpHeaders-获取请求头"><a href="#使用-HttpHeaders-获取请求头" class="headerlink" title="使用 HttpHeaders 获取请求头"></a>使用 HttpHeaders 获取请求头</h4><pre><code class="java">@PostMapping(&quot;/getAuthorizationByHeaders&quot;)
public String getAuthorizationByHeaders(@RequestHeader HttpHeaders headers) &#123;

    List&lt;String&gt; authorization = headers.get(&quot;Authorization&quot;);

    log.info(&quot;获取 Authorization ---&gt;&#123;&#125;&quot;, authorization);

    return &quot;SUCCESS&quot;;
&#125;</code></pre>
<h4 id="使用-HttpServletRequest-获取"><a href="#使用-HttpServletRequest-获取" class="headerlink" title="使用 HttpServletRequest 获取"></a>使用 HttpServletRequest 获取</h4><pre><code class="java">@PostMapping(&quot;/getAuthorizationByServlet&quot;)
public String getAuthorizationByServlet(HttpServletRequest request) &#123;

    String authorization = request.getHeader(&quot;Authorization&quot;);

    log.info(&quot;获取 Authorization ---&gt;&#123;&#125;&quot;, authorization);

    return authorization;
&#125;</code></pre>
<h4 id="测试文件"><a href="#测试文件" class="headerlink" title="测试文件"></a>测试文件</h4><p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/dqAEES-LSudFy.png" alt="dqAEES-LSudFy"></p>
<p>经过测试这些都是可以的，最终选择使用 Map 接收 Header ，然后从 Map 中获取 Authorization。</p>
<blockquote>
<p>PS: 可能有小伙伴测试不过，发现接受的 header 里的 name 全都是小写了，可以继续阅读。<br>源码在文末，也可以关注公众号，发送 headerName/4 获取。</p>
</blockquote>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/rXC2fS-cVftVR.jpg" alt="rXC2fS-cVftVR"></p>
<p>你以为事情如果到这里就结束了，那真是太天真了。</p>
<p>这不，出现了文章开头的描述的场景，赶紧回滚，然后排查问题，最后定位到是 Header 的 name 大小写问题。</p>
<h4 id="思考"><a href="#思考" class="headerlink" title="思考"></a>思考</h4><ol>
<li>之前 APP 端也是这么传的，那为什么使用拦截器是正常的呢？</li>
<li>上面的那几种方式是不是都是这样？</li>
<li>不排除 tomcat 发现原来都会转换为小写，又是为什么？</li>
</ol>
<h3 id="模拟排查"><a href="#模拟排查" class="headerlink" title="模拟排查"></a>模拟排查</h3><h4 id="环境配置"><a href="#环境配置" class="headerlink" title="环境配置"></a>环境配置</h4><p>模拟生产首先使用相同的容器配置，这里排除了内置的 tomcat 容器，并且使用 undertow 容器。</p>
<pre><code class="xml">&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;exclusions&gt;
        &lt;exclusion&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
        &lt;/exclusion&gt;
        &lt;!-- Exclude the Tomcat dependency --&gt;
        &lt;exclusion&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
        &lt;/exclusion&gt;
    &lt;/exclusions&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-undertow&lt;/artifactId&gt;
&lt;/dependency&gt;</code></pre>
<h4 id="使用拦截器传小写为什么没有问题"><a href="#使用拦截器传小写为什么没有问题" class="headerlink" title="使用拦截器传小写为什么没有问题"></a>使用拦截器传小写为什么没有问题</h4><ul>
<li>修改使用小写 <code>authorization</code></li>
</ul>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/BnwZnf-7AvYYg.png" alt="BnwZnf-7AvYYg"></p>
<ul>
<li>debug 断点</li>
</ul>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/OOjOyV-54K6Na.png" alt="OOjOyV-54K6Na"></p>
<p>神奇的一幕出现了，收到的确实是小写，但是 <strong>request.getHeader(“Authorization”);</strong> 却可以获取到 <code>authorization</code></p>
<ul>
<li>F7 继续往里跟</li>
</ul>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/JwgnFS-cW3pJY.png" alt="JwgnFS-cW3pJY"></p>
<p><code>io.undertow.servlet.spec.HttpServletRequestImpl#getHeader</code> 第 190 行，从 HeaderMap 中获取第一个元素</p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/DnRSum-crs0Mb.png" alt="DnRSum-crs0Mb"></p>
<p><code>io.undertow.util.HeaderMap#getFirst</code> 第 297 行， 通过 getEntry 方法获取 header</p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/C8lSGw-XxKDPW.png" alt="C8lSGw-XxKDPW"></p>
<p>继续追踪，发现在 <code>io.undertow.util.HeaderMap#getEntry(java.lang.String)</code> 方法 77~79 行的时候获取到了 header 信息。那就看一下这块的源码吧。</p>
<p>在仔细看一下发现是 77 行 <code>final int hc = HttpString.hashCodeOf(headerName);</code> 在获取 name 的 hashCode 时，这里无论大小写，都是同一个 hashCode。这块代码如下</p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/3v1FVJ-MuPXUA.png" alt="3v1FVJ-MuPXUA"></p>
<p>higher 方法：</p>
<pre><code class="java">private static int higher(byte b) &#123;
    return b &amp; (b &gt;= &#39;a&#39; &amp;&amp; b &lt;= &#39;z&#39; ? 0xDF : 0xFF);
&#125;</code></pre>
<p>这块的含义</p>
<ol>
<li>如果 b 是小写字符则 <code>b &amp; 0xDF</code></li>
<li>如果 b 是大写字符则 <code>b &amp; 0xFF</code></li>
</ol>
<p>对照 ASCII 表，大小写字母相差 32 而 0xFF(255) 和 0xDF(223) 同样相差 32，所以问题定位到了。header 的 name 无论是大写还是小写，都会查出同一个值。</p>
<p>当然你也可以这么传</p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/VOOF8k-LoGjAf.png" alt="VOOF8k-LoGjAf"></p>
<p>这样的话谁在上面，Header 中使用的 name 就是那个。</p>
<h4 id="使用-Map-为什么会区分大小写"><a href="#使用-Map-为什么会区分大小写" class="headerlink" title="使用 Map 为什么会区分大小写"></a>使用 Map 为什么会区分大小写</h4><p>传入的是大写</p>
<pre><code class="text">HttpServlet 
-&gt; DispatcherServlet#doDispatch 
-&gt; AbstractHandlerMethodAdapter#handle 
-&gt; RequestMappingHandlerAdapter#handleInternal 
-&gt; RequestMappingHandlerAdapter#invokeHandlerMethod 
-&gt; ServletInvocableHandlerMethod#invokeAndHandle
-&gt; InvocableHandlerMethod#invokeForRequest (解析参数值)
-&gt; InvocableHandlerMethod#getMethodArgumentValues
-&gt; RequestHeaderMapMethodArgumentResolver#resolveArgument</code></pre>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/dqAEES-LSudFy.png" alt="dqAEES-LSudFy"></p>
<p>如图所示 <code>String headerName = iterator.next();</code> name 被区分大小写放到了 LinkedHashMap 中，后续会反射调用对应的 Controller 方法。</p>
<p>所以也就出现了我所遇到的问题。 </p>
<p>当然理论上 APP 客户端不应该测试和预发布使用大写，而生产使用小写。</p>
<p>上面阅读的源码只是 Spring 对 Header 的处理，Spring 在 <code>HttpServlet</code> 收到请求时，Spring 没有对请求 Header 的 name 大小写进行转换，只是在获取对应 value 的时候，没有区分大小写进行获取。</p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/mpl3PK-m7dKAb.jpg"></p>
<h3 id="容器对-header-的处理"><a href="#容器对-header-的处理" class="headerlink" title="容器对 header 的处理"></a>容器对 header 的处理</h3><h5 id="undertow-容器的处理"><a href="#undertow-容器的处理" class="headerlink" title="undertow 容器的处理"></a>undertow 容器的处理</h5><ul>
<li>请求参数的处理</li>
</ul>
<p>这里发现 undertow 并没有对请求参数进行大小写转换处理操作。</p>
<ul>
<li>从 HttpServletRequest 获取 header</li>
</ul>
<p>debug 发现调用的是 <code>io.undertow.servlet.spec.HttpServletRequestImpl#getHeader</code>，这个过程就是上面的排查过程。</p>
<ul>
<li>从 Headers 中获取 header</li>
</ul>
<p>通过 debug 发现 jetty 调用的是 <code>org.springframework.http.HttpHeaders#get</code>，然后调用 <code>org.springframework.util.MultiValueMapAdapter#get</code>，然后调用 <code>org.springframework.util.LinkedCaseInsensitiveMap#get</code></p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/nRw6xN-GkUqAN.png" alt="nRw6xN-GkUqAN"></p>
<p>这里会不区分大小写</p>
<ul>
<li>从 MultiValueMap 获取 header</li>
</ul>
<p>这块 debug 发现是直接从 <code>LinkedHashMap</code> 获取的，所以区分了大小写。</p>
<h4 id="tomcat-容器的处理"><a href="#tomcat-容器的处理" class="headerlink" title="tomcat 容器的处理"></a>tomcat 容器的处理</h4><ul>
<li>请求参数的处理</li>
</ul>
<p>而如果没有排除的话，即使用内嵌的 tomcat 容器无论传递大写还是小写，接收到的全部都是小写，又是怎么个情况呢？</p>
<p>通过 debug 发现没有排除 tomcat 使用的是，在接收请求时使用的是 <code>org.apache.coyote.http11.Http11Processor</code>。</p>
<p>在 <code>Http11Processor#service</code> 方法中</p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/303mmp-6TczCR.png" alt="303mmp-6TczCR"></p>
<p>类 284 行负责处理解析 header </p>
<p>进入 <code>org.apache.coyote.http11.Http11InputBuffer#parseHeaders</code> 方法</p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/cqHTEZ-1E9mH7.png" alt="cqHTEZ-1E9mH7"></p>
<p>第 589 行 （Download Sources 后），阅读 <code>parseHeader</code> 方法</p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/oEEbej-rMulR7.png" alt="oEEbej-rMulR7"></p>
<p>发现会将请求 header 的 name 转换为小写</p>
<ul>
<li>从 HttpServletRequest 获取 header</li>
</ul>
<p>当使用 tomcat 容器时，调用 <code>org.apache.catalina.connector.RequestFacade#getHeader</code>， <code>org.apache.catalina.connector.Request#getHeader</code>， <code>org.apache.coyote.Request#getHeader</code> <code>org.apache.tomcat.util.http.MimeHeaders#getHeader</code> 最后调用 <code>org.apache.tomcat.util.http.MimeHeaders#getValue</code> 获取 header </p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/qDJXXn-z6hTjZ.png" alt="qDJXXn-z6hTjZ"></p>
<p>这里也会忽略大小写判断</p>
<ul>
<li>从 Headers 获取 header</li>
</ul>
<p>通过 debug 发现 tomcat 容器下调用的是 <code>org.springframework.http.HttpHeaders#get</code>，然后调用 <code>org.springframework.util.MultiValueMapAdapter#get</code>，然后调用 <code>org.springframework.util.LinkedCaseInsensitiveMap#get</code></p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/nRw6xN-GkUqAN.png" alt="nRw6xN-GkUqAN"></p>
<p>这里会不区分大小写</p>
<ul>
<li>从 MultiValueMap 获取 header</li>
</ul>
<p>这块 debug 发现是直接从 <code>LinkedHashMap</code> 获取的，所以区分了大小写。</p>
<h4 id="jetty-容器的处理"><a href="#jetty-容器的处理" class="headerlink" title="jetty 容器的处理"></a>jetty 容器的处理</h4><ul>
<li>请求参数的处理</li>
</ul>
<p>如果换成 jetty 容器的话</p>
<p>在 <code>org.eclipse.jetty.server.HttpConnection</code> 中又会发现无论传入大写还是小写都会被转换为驼峰。</p>
<p>源码可以阅读 <code>org.eclipse.jetty.http.HttpParser#parseFields</code></p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/Q9KjmT-HSRusz.png" alt="Q9KjmT-HSRusz"></p>
<p>会转换为驼峰命名法。</p>
<ul>
<li>从 HttpServletRequest 获取 header</li>
</ul>
<p>通过 debug 发现 jetty 调用的是 <code>org.eclipse.jetty.server.Request#getHeader</code></p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/H9onCc-uyaMMn.png" alt="H9onCc-uyaMMn"></p>
<p>jetty 在获取 header 时，会调用 <code>org.eclipse.jetty.http.HttpFields#get</code></p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/BOkxFw-op6ZIV.png" alt="BOkxFw-op6ZIV"></p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/ioe7pS-uNuRU6.png" alt="ioe7pS-uNuRU6"></p>
<p>原来在获取的时候忽略了大小写</p>
<ul>
<li>从 Headers 获取 header</li>
</ul>
<p>通过 debug 发现 jetty 容器下调用的是 <code>org.springframework.http.HttpHeaders#get</code>，然后调用 <code>org.springframework.util.MultiValueMapAdapter#get</code>，然后调用 <code>org.springframework.util.LinkedCaseInsensitiveMap#get</code></p>
<p><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/nRw6xN-GkUqAN.png" alt="nRw6xN-GkUqAN"></p>
<p>这里会不区分大小写</p>
<ul>
<li>从 MultiValueMap 获取</li>
</ul>
<p>也是调用的 <code>org.springframework.util.MultiValueMapAdapter#get</code> 然后不区分大小写。和从 Headers 中获取相同。</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><h4 id="Q-amp-A"><a href="#Q-amp-A" class="headerlink" title="Q&amp;A"></a>Q&amp;A</h4><p><strong>Q: 为什么拦截器获取 Authorization 可以不区分大小写？</strong></p>
<p><strong>A:</strong> 从拦截器获取 Authorization 其实就是从 <code>HttpServletRequest</code> 中获取，这里无论使用 tomcat 还是使用 undertow 或者 jetty 获取 Header 是都是忽略 headerName 的大小写的。具体可以阅读上面的源码分析。</p>
<p><strong>Q: 这么多获取 Header 的方式有什么区别？</strong><br><strong>A:</strong></p>
<p>不同的容器下实现方式不同，这里列表说明</p>
<table>
<thead>
<tr>
<th align="center"></th>
<th align="center">undertow</th>
<th align="center">tomcat</th>
<th align="center">jetty</th>
</tr>
</thead>
<tbody><tr>
<td align="center">请求参数大小写转换</td>
<td align="center">不变</td>
<td align="center">小写</td>
<td align="center">驼峰</td>
</tr>
<tr>
<td align="center">直接获取请求头某一个 headerName</td>
<td align="center">忽略大小写，不能为空</td>
<td align="center">忽略大小写，不能为空</td>
<td align="center">忽略大小写，不能为空</td>
</tr>
<tr>
<td align="center">使用 Map 获取所有请求头</td>
<td align="center">Map 的 key 和传入 headerName 大小写的一致，保持一致可获取到</td>
<td align="center">Map 的 key 全是小写，需要使用小写headerName 获取</td>
<td align="center">Map 的 key 是驼峰命名法，要使用驼峰命名才可以获取到</td>
</tr>
<tr>
<td align="center">使用 MultiValueMap 获取请求头</td>
<td align="center">实际是从 LinkedHashMap 中获取，区分大小写</td>
<td align="center">实际是从 LinkedHashMap 中获取，区分大小写</td>
<td align="center">从 LinkedCaseInsensitiveMap 获取，不区分大小写</td>
</tr>
<tr>
<td align="center">使用 HttpHeaders 获取请求头</td>
<td align="center">从 LinkedCaseInsensitiveMap 获取，不区分大小写</td>
<td align="center">从 LinkedCaseInsensitiveMap 获取，不区分大小写</td>
<td align="center">从 LinkedCaseInsensitiveMap 获取，不区分大小写</td>
</tr>
<tr>
<td align="center">使用 HttpServletRequest 获取</td>
<td align="center">使用 HttpString.hashCodeOf(headerName) 忽略了大小写</td>
<td align="center">调用 MimeHeaders#getValue 忽略了大小写</td>
<td align="center">HttpFields#get 忽略了大小写</td>
</tr>
</tbody></table>
<p>通过表格发现，即使是不同的容器在使用 HttpHeaders 获取请求头是都是调用了 Spring 的 <code>LinkedCaseInsensitiveMap</code> 获取 header，并且内部忽略了大小写，这里比较推荐使用。</p>
<p>同样使用 HttpServletRequest 的方式获取也比较推荐。</p>
<h4 id="结束语"><a href="#结束语" class="headerlink" title="结束语"></a>结束语</h4><p>本文主要是分析生产遇到的一个问题，然后开始探究原因，开始的时候发现是 Spring 的原因，因为使用 Map 接收时， headerName 什么格式就是什么格式。</p>
<p>在自己写 demo 时又发现，原来和 Spring 的关系并不大，是容器的原因。不同的容器处理方式不同。所以总结出来相关文章，供大家参考，不足之处，欢迎指正。</p>
<h4 id="相关资料"><a href="#相关资料" class="headerlink" title="相关资料"></a>相关资料</h4><ol>
<li>本文源码地址：<a target="_blank" rel="noopener" href="https://github.com/liuzhihang/header-demo">https://github.com/liuzhihang/header-demo</a></li>
</ol>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">liuzhihang</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://liuzhihang.com/2020/10/19/header-name-case.html">https://liuzhihang.com/2020/10/19/header-name-case.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://liuzhihang.com" target="_blank">程序员小航</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%B7%A5%E4%BD%9C%E7%AC%94%E8%AE%B0/">工作笔记</a></div><div class="post_share"><div class="social-share" data-image="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/app-header-ydHubh.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/10/25/source-code-concurrentlinkedqueue.html"><img class="prev-cover" src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/concurrentlinkedqueue-6pf8RL.jpeg" onerror="onerror=null;src='https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/feature/92776_n5aac6.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【JDK源码笔记】- 非阻塞的无界线程安全队列 —— ConcurrentLinkedQueue</div></div></a></div><div class="next-post pull-right"><a href="/2020/10/18/log-trace-id.html"><img class="next-cover" src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/log-trace-cover-LnUPB9.jpeg" onerror="onerror=null;src='https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/feature/92776_n5aac6.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【工作笔记】- 几行代码轻松实现跨系统传递 traceId，再也不用担心对不上日志了！</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/08/23/technology-sharing-es-2.html" title="【Elasticsearch 技术分享】—— ES 常用名词及结构"><img class="cover" src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/es-3-X6uCwo.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-23</div><div class="title">【Elasticsearch 技术分享】—— ES 常用名词及结构</div></div></a></div><div><a href="/2020/08/26/technology-sharing-es-3.html" title="【Elasticsearch 技术分享】—— Elasticsearch 存储一条数据， put 过程是什么样子的？"><img class="cover" src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/es-3-lGaP9k.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-26</div><div class="title">【Elasticsearch 技术分享】—— Elasticsearch 存储一条数据， put 过程是什么样子的？</div></div></a></div><div><a href="/2020/08/18/technology-sharing-es-1.html" title="【Elasticsearch 技术分享】—— Elasticsearch ？倒排索引？这都是什么？"><img class="cover" src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/es-1-JhMblx.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-18</div><div class="title">【Elasticsearch 技术分享】—— Elasticsearch ？倒排索引？这都是什么？</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/404/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">liuzhihang</div><div class="author-info__description">学，然后知不足；教，然后知困。</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">145</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">55</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">31</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/liuzhihang"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/liuzhihang" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=Vjo-Iyw_Pz43ODElFicneDU5Ow" target="_blank" title="Email"><i class="fas fa-envelope-open"></i></a><a class="social-icon" href="https://weibo.com/onlyhang" target="_blank" title="Weibo"><i class="fab fa-weibo"></i></a><a class="social-icon" href="https://twitter.com/liuzhihangs" target="_blank" title="Twitter"><i class="fab fa-twitter"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="RSS"><i class="fas fa-rss"></i></a><a class="social-icon" href="https://www.infoq.cn/u/liuzhihang/publish" target="_blank" title="InfoQ"><i class="iconfont iconweibiaoti-1"></i></a><a class="social-icon" href="https://juejin.im/user/1987506650493117" target="_blank" title="掘金"><i class="iconfont iconjuejin-1"></i></a><a class="social-icon" href="https://blog.csdn.net/qq_36535538" target="_blank" title="CSDN"><i class="iconfont iconcsdn"></i></a><a class="social-icon" href="https://www.zhihu.com/people/liuzhihang" target="_blank" title="知乎"><i class="iconfont iconzhihu1"></i></a><a class="social-icon" href="https://leetcode-cn.com/u/liuzhihang" target="_blank" title="LeetCode"><i class="iconfont iconleetcode"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">🧑‍💻感谢访问本站，若喜欢请收藏 ^_^ <br> <br> <img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/wechat.jpg"> <br> 个人公众号:『 程序员小航 』</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%83%8C%E6%99%AF"><span class="toc-text">背景</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8E%9F%E6%B5%81%E7%A8%8B"><span class="toc-text">原流程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%B0%E9%9C%80%E6%B1%82"><span class="toc-text">新需求</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%8E-Header-%E4%B8%AD%E8%8E%B7%E5%8F%96%E4%BF%A1%E6%81%AF"><span class="toc-text">从 Header 中获取信息</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%9B%B4%E6%8E%A5%E8%8E%B7%E5%8F%96%E8%AF%B7%E6%B1%82%E5%A4%B4%E6%9F%90%E4%B8%80%E4%B8%AA-headerName"><span class="toc-text">直接获取请求头某一个 headerName</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-Map-%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E8%AF%B7%E6%B1%82%E5%A4%B4"><span class="toc-text">使用 Map 获取所有请求头</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-MultiValueMap-%E8%8E%B7%E5%8F%96%E8%AF%B7%E6%B1%82%E5%A4%B4"><span class="toc-text">使用 MultiValueMap 获取请求头</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-HttpHeaders-%E8%8E%B7%E5%8F%96%E8%AF%B7%E6%B1%82%E5%A4%B4"><span class="toc-text">使用 HttpHeaders 获取请求头</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-HttpServletRequest-%E8%8E%B7%E5%8F%96"><span class="toc-text">使用 HttpServletRequest 获取</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6"><span class="toc-text">测试文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%80%9D%E8%80%83"><span class="toc-text">思考</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A8%A1%E6%8B%9F%E6%8E%92%E6%9F%A5"><span class="toc-text">模拟排查</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE"><span class="toc-text">环境配置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E6%8B%A6%E6%88%AA%E5%99%A8%E4%BC%A0%E5%B0%8F%E5%86%99%E4%B8%BA%E4%BB%80%E4%B9%88%E6%B2%A1%E6%9C%89%E9%97%AE%E9%A2%98"><span class="toc-text">使用拦截器传小写为什么没有问题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-Map-%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BC%9A%E5%8C%BA%E5%88%86%E5%A4%A7%E5%B0%8F%E5%86%99"><span class="toc-text">使用 Map 为什么会区分大小写</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E5%AF%B9-header-%E7%9A%84%E5%A4%84%E7%90%86"><span class="toc-text">容器对 header 的处理</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#undertow-%E5%AE%B9%E5%99%A8%E7%9A%84%E5%A4%84%E7%90%86"><span class="toc-text">undertow 容器的处理</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#tomcat-%E5%AE%B9%E5%99%A8%E7%9A%84%E5%A4%84%E7%90%86"><span class="toc-text">tomcat 容器的处理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#jetty-%E5%AE%B9%E5%99%A8%E7%9A%84%E5%A4%84%E7%90%86"><span class="toc-text">jetty 容器的处理</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-text">总结</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Q-amp-A"><span class="toc-text">Q&amp;A</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%93%E6%9D%9F%E8%AF%AD"><span class="toc-text">结束语</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E8%B5%84%E6%96%99"><span class="toc-text">相关资料</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/09/04/the_converter_converts_front_end_parameters_to_enumerations.html" title="使用 SpringBoot 转换器将前端参数转换为枚举"><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/uztio4-T5n5Wm.jpg" onerror="this.onerror=null;this.src='https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/feature/92776_n5aac6.jpg'" alt="使用 SpringBoot 转换器将前端参数转换为枚举"/></a><div class="content"><a class="title" href="/2021/09/04/the_converter_converts_front_end_parameters_to_enumerations.html" title="使用 SpringBoot 转换器将前端参数转换为枚举">使用 SpringBoot 转换器将前端参数转换为枚举</a><time datetime="2021-09-03T23:00:00.000Z" title="发表于 2021-09-04 07:00:00">2021-09-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/08/22/guide_to_mapper_overloading_crater_in_mybatis_plus.html" title="MyBatis-Plus 中 Mapper 重载踩坑指南"><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/ze1d2z-wXDWp0.jpg" onerror="this.onerror=null;this.src='https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/feature/92776_n5aac6.jpg'" alt="MyBatis-Plus 中 Mapper 重载踩坑指南"/></a><div class="content"><a class="title" href="/2021/08/22/guide_to_mapper_overloading_crater_in_mybatis_plus.html" title="MyBatis-Plus 中 Mapper 重载踩坑指南">MyBatis-Plus 中 Mapper 重载踩坑指南</a><time datetime="2021-08-21T23:00:00.000Z" title="发表于 2021-08-22 07:00:00">2021-08-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/07/25/tips_for_locking_and_optimizing_concurrent_scenes.html" title="并发场景加锁优化小技巧"><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/MODJ8j-9ObHeB.jpg" onerror="this.onerror=null;this.src='https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/feature/92776_n5aac6.jpg'" alt="并发场景加锁优化小技巧"/></a><div class="content"><a class="title" href="/2021/07/25/tips_for_locking_and_optimizing_concurrent_scenes.html" title="并发场景加锁优化小技巧">并发场景加锁优化小技巧</a><time datetime="2021-07-24T23:00:00.000Z" title="发表于 2021-07-25 07:00:00">2021-07-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/07/16/curator-5.html" title="ZooKeeper 分布式锁 Curator 源码 05：分布式读写锁和联锁"><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/EO0tci-8Ju4bN.png" onerror="this.onerror=null;this.src='https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/feature/92776_n5aac6.jpg'" alt="ZooKeeper 分布式锁 Curator 源码 05：分布式读写锁和联锁"/></a><div class="content"><a class="title" href="/2021/07/16/curator-5.html" title="ZooKeeper 分布式锁 Curator 源码 05：分布式读写锁和联锁">ZooKeeper 分布式锁 Curator 源码 05：分布式读写锁和联锁</a><time datetime="2021-07-16T13:30:30.000Z" title="发表于 2021-07-16 21:30:30">2021-07-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/07/15/curator-4.html" title="ZooKeeper 分布式锁 Curator 源码 04：分布式信号量和互斥锁"><img src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/article/EO0tci-8Ju4bN.png" onerror="this.onerror=null;this.src='https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/feature/92776_n5aac6.jpg'" alt="ZooKeeper 分布式锁 Curator 源码 04：分布式信号量和互斥锁"/></a><div class="content"><a class="title" href="/2021/07/15/curator-4.html" title="ZooKeeper 分布式锁 Curator 源码 04：分布式信号量和互斥锁">ZooKeeper 分布式锁 Curator 源码 04：分布式信号量和互斥锁</a><time datetime="2021-07-15T13:30:30.000Z" title="发表于 2021-07-15 21:30:30">2021-07-15</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2017 - 2021 By liuzhihang</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><img class="icp-icon" src= "https://cdn.jsdelivr.net/gh/liuzhihang/oss/pic/loading.gif" data-lazy-src="/resources/image/icp.png"><span>备案号：京ICP备20000888号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      false && mermaid.init()
    })
  }
}</script><script>function addGitalkSource () {
  const ele = document.createElement('link')
  ele.rel = 'stylesheet'
  ele.href= 'https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.css'
  document.getElementsByTagName('head')[0].appendChild(ele)
}

function loadGitalk () {
  function initGitalk () {
    var gitalk = new Gitalk(Object.assign({
      clientID: 'e48f300349e2ac2d03bd',
      clientSecret: '001c669e6269dfd9d7d9fef029cc71fd00ea69e3',
      repo: 'comments',
      owner: 'liuzhihang',
      admin: ['liuzhihang'],
      id: 'c32427b4bac9f86c33ac10d01e7a7985',
      language: 'en',
      perPage: 10,
      distractionFreeMode: false,
      pagerDirection: 'last',
      createIssueManually: false,
      updateCountCallback: commentCount
    },null))

    gitalk.render('gitalk-container')
  }

  if (typeof Gitalk === 'function') initGitalk()
  else {
    addGitalkSource()
    getScript('https://cdn.jsdelivr.net/npm/gitalk@latest/dist/gitalk.min.js').then(initGitalk)
  }
}

function commentCount(n){
  let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
  if (isCommentCount) {
    isCommentCount.innerHTML= n
  }
}

if ('Gitalk' === 'Gitalk' || !false) {
  if (false) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
  else loadGitalk()
} else {
  function loadOtherComment () {
    loadGitalk()
  }
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>